export default {
  install(Vue) {
    Vue.prototype.api = {
      baseUrlData: "https://apis.netstart.cn",
      // baseUrlData: "/data",
      baseUrlV2: "/v2",
      time: Date.now(),
      //加入书架
      payBookself: (book, what) => {
        let bookselfList = JSON.parse(localStorage.getItem("bookselfList")) || [];
        if (!what) {
          bookselfList = [book, ...bookselfList];
        } else {
          bookselfList = [
            ...bookselfList.filter((item) => item.id != book.id),
          ];
        }
        localStorage.setItem("bookselfList", JSON.stringify(bookselfList));
      },
      //跳书籍详情,产生历史搜索记录用
      toDetail(item, self, type) {
        console.log(item, '1w2');
        if (item) {
          item.id = item.id || item.bookId;
          item.title = item.text || item.name || item.title;
        }
        if (type && type !== "clear") {
          let list = JSON.parse(localStorage.getItem("historySearch")) || [];
          list = [item, ...list.filter(el => el.id != item.id)];
          localStorage.setItem("historySearch", JSON.stringify(list));
        } else if (type && type === "clear") {
          localStorage.setItem('historySearch', '[]');
          return;
        }
        self.$router.push({
          name: "detail",
          params: {
            bookid: item.id || item.bookId,
            title: item.text || item.name || item.title,
          },
        });
      },
      //阅读记录
      changeReading({
        bookInfo,
        page
      }) {
        bookInfo.id = bookInfo.id || bookInfo.sourceUuid;
        let readHistory = JSON.parse(localStorage.getItem("readHistory")) || [];
        readHistory = [{
          bookInfo,
          page
        }, ...readHistory.filter(item => item.bookInfo.id != bookInfo.id)];
        localStorage.setItem("readHistory", JSON.stringify(readHistory));
      }
    }
  }
}